﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inspired.Euler
{
    public static class Problem030
    {
        public static long Solve()
        {
            //String.Join(", ", 28L.Divisors()).DisplayAndPause();

            int power = 5;
            // quantidade máxima de dígitos = 5
            // valor máximo por dígito: 9^5
            // maior valor possível
            double maxValue = (power + 1) * (Math.Pow(9, 5)) - 1;
            List<double> fifths = new List<double>();
            for (double i = 2; i <= maxValue; i++)
            {
                double candidate = 0;
                foreach (char number in i.ToString())
                {
                    candidate += Math.Pow(Convert.ToInt32(number.ToString()), power);
                }
                if (candidate == i)
                {
                    fifths.Add(candidate);
                }
            }
            return (long)fifths.Sum();
        }
    }
}
